//
//  ZXXVistorView.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/18.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit
import SnapKit

let Margin = 16
let ButtonHeight = 36
let ButtonWidth = 100
let TextSize:CGFloat = 14
let MessageLabelWidth = 230
let MessageLabelHeight = 36

class ZXXVistorView: UIView {
//    定义一个block回调
    var visitorClosure:(()->())?

// // MARK: - 添加子控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  让小圈圈转起来
    private func setCircleAnim(){
//        定义一个anim
        let anim = CABasicAnimation(keyPath: "transform.rotation")
//        设置动画执行事件
//        动画时长
        anim.duration = 15
//        动画次数
        anim.repeatCount = MAXFLOAT
//        动画toValue
        anim.toValue = 2 * M_PI
//        当我们切换tabbar 或者程序退到后台 默认动画被移除
        anim.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(anim, forKey: nil )
    }
    
    
    // MARK: -  监听事件 注册和登录的按钮被点击时
    @objc private func loginClick(){
        visitorClosure?()
    }
    
    // MARK: - 外界调用方法 确定是哪一个tableBar被点击的
    func setVisitorInfo(imgName: String?, title: String?){
        
        // 消息 发现 我
        if let img = imgName, tit = title{
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            // 隐藏圆圈
            circleImageView.hidden = true
        }else{
            // 代表首页
            setCircleAnim()
        }
        
    }

    
   // MARK: -  子控件的创建
    func setupUI(){
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        // 01 添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 02 添加约束
        // 01 圆圈图
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        // 02遮盖图
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 03 小房子图
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 04 文字Label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(Margin)
            make.centerX.equalTo(maskImageView)
            make.height.equalTo(MessageLabelHeight)
            make.width.equalTo(MessageLabelWidth)
        }
        // 05 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(Margin)
            make.size.equalTo(CGSize(width: ButtonWidth, height: ButtonHeight))
        }
        // 06 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: ButtonWidth, height: ButtonHeight))
        }

        
    }
    
    
    // MARK: - 懒加载控件
    // 圆圈
    private lazy var circleImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮挡
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
    }()
    // 小房子
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 文字
    private lazy var messageLabel:UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        // 设置换行
        lab.numberOfLines = 0
        // 对齐方式
        lab.textAlignment = .Center
        lab.font = UIFont.systemFontOfSize(TextSize)
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return lab
    }()
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        // 点击事件
        button.addTarget(self, action: #selector(ZXXVistorView.loginClick), forControlEvents: UIControlEvents.TouchUpInside)
        // 设置button 背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 添加title
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置颜色
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        // 设置font
        button.titleLabel?.font = UIFont.systemFontOfSize(TextSize)
        return button
    }()
    
    // 登陆按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        // 点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置button 背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 添加title
        button.setTitle("登陆", forState: UIControlState.Normal)
        // 设置颜色
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        // 设置font
        button.titleLabel?.font = UIFont.systemFontOfSize(TextSize)
        return button
    }()

}
